package com.chinasofti.manager.dao;

import com.chinasofti.manager.domain.Teacher;

public class TeacherDao {
    //创建老师数组
    private static Teacher[] tchs = new Teacher[5];
    public int getIndex(String id) {
        int index= -1;
        for (int i = 0; i <tchs.length ; i++) {
         Teacher stu = tchs[i];
            if(stu!=null && stu.getId().equals(id)){
                index=i;
                break;
            }
        }
        return index;
    }
        public boolean addTeacher(Teacher tch) {
            //添加学生到数组中
            int index = -1;//假设数组已经存满
            for (int i = 0; i <tchs.length ; i++) {
                Teacher student = tchs[i];
                if (student==null) {//如果数组没装满
                    index = i;//用index记录当前下标
                    break;
                }
            }
            if (index==-1){//装满了
                return  false;
            }else {//没装满,并且将学生对象添加到数组
                tchs[index] = tch;
                return true;
            }
    }
    public Teacher[] findAllTeacher() {
        return  tchs;
    }

    public void deleteTeacherById(String id) {
        int index = getIndex(id);//查询被删除id索引的位置在不在
        tchs[index]=null;//将索引位置使用null覆盖
    }

    public void updateTeacher(String updateId, Teacher newTch) {
        int index = getIndex(updateId);
       tchs[index] = newTch;
    }
}


